import { placeOrder } from '@/sub_pages/movie/common/api/movie.js';
export default {
    //生成新订单并获取订单信息
    async getPlaceOrder(vm) {
        try {
            let data = JSON.parse(JSON.stringify(getApp().globalData.subcontracting_movie.confirmOrder?.orderParams || {}));
            data.seats = this.getSeatsList(data.seats);
            if (getApp().globalData.subcontracting_movie.confirmOrder && getApp().globalData.subcontracting_movie.confirmOrder.ticketType) {
                data.ticketType = getApp().globalData.subcontracting_movie.confirmOrder.ticketType;
            }

            let params = data;
            if (vm.options.orderId) {
                params.orderId = vm.options.orderId;
            }

            // 购票方式 0.特惠购票 1.快捷出票
            params.deliverMode = params.ticketType == 1 ? 0 : 1;

            let res = await placeOrder(params);

            vm.orderModel = res.data;
            vm.orderModel.seatCount = params.seats.split(',').length;
        } catch (error) {
            uni.$u.sleep(800).then(() => {
                uni.navigateBack();
            });
        }
    },
    // 初始化选中优惠券
    getInitCoupons(vm) {
        if (!vm.options.orderId) {
            let data = getApp().globalData.subcontracting_movie.maxCoupon;
            if (data.discountCouponId) {
                vm.usedSelectCoupons = [data];
                vm.usedCouponType = this.watchUsedCouponType(vm.usedSelectCoupons);
            }
        }
    },
    watchUsedCouponType(source) {
        let type = 0;
        source.forEach(r => {
            if (r.thresholdDiscount != 0) {
                type = r.thresholdDiscount;
            }
        });
        return type;
    },
    getSeatsList(e) {
        let list = [];
        if (e) {
            e = JSON.parse(e);
            e.forEach(r => {
                list.push(r.Name);
            });
        }
        return list.join(',') || '';
    },
    // 统计可选择优惠券数量
    async getPayCouponSelectCount(vm) {
        let para = {
            thresholdUsed: '1',
        };
        try {
            let res = (await vm.$api.getPayCouponSelectCount(para)) || 0;
            vm.couponCount = Number(res) || 0;
        } catch (e) {
            //TODO handle the exception
            vm.couponCount = 0;
        }
    },
    // 获取优惠券抵扣金额
    getDeductionPrice(source, data) {
        if (source.length > 0) {
            let price = Number(data.ticketAmount);
            // let signleAmount = Number(data.ticketAmount / data.seatCount) || 0
            let amount = 0;
            source.forEach(r => (amount += Number(r.saveAmount)));
            if (price > amount) {
                return amount;
            }
            return price;
        }
        return 0;
    },
    /**
     * type 返回是否选择优惠券
     * 0：未选择优惠券或电影卡
     * 1：用户选择了兑换券
     * 2：用户选择了充值卡
     * 3：用户选择了充值卡和兑换券
     */
    usedPayMethodType(vm) {
        let type = 0;
        if (vm.selctCouponCards.length > 0) type = 1;
        return type;
    },
    // 兑换券id获取
    getCopupCardIds(source) {
        let ids = [];
        if (source) {
            source.forEach(e => ids.push(e.couponId));
        }
        return ids.join(',');
    },
    getCouponIds(source) {
        let ids = [];
        if (source) {
            source.forEach(r => ids.push(r.discountCouponId));
        }
        return ids.join(',');
    },

    // 微信支付
    wxPay(param) {
        return new Promise((resolve, reject) => {
            uni.requestPayment({
                provider: 'wxpay',
                timeStamp: param.timeStamp,
                nonceStr: param.nonceStr,
                package: param.packageValue,
                signType: param.signType,
                paySign: param.paySign,
                success: res => {
                    resolve(res);
                },
                fail: err => {
                    reject(err);
                },
            });
        });
    },
};
